var album_language="推荐";
var album_area_choice=null;
var page = 1;
var orderType = "最新";
var last_page=null;
//选择语种
$(function () {
    $("#click_show ul li a ").click(function () {
        album_language =$(this).attr("value");
        $("#area_title ul li a").html(album_language);
        if(album_language == "推荐"){
            //alert("true");
            $.ajax({
                "url":"/AlbumPageServlet?opr=recommand",
                "data":"page="+page,
                "type":"POST",
                "dataType":"json",
                "success":function (data) {
                    // alert(eval(data.length));
                    $("#clearDIV .album_item_1").empty();
                    $("#clearDIV .album_item_2").empty();
                    $("#clearDIV .img_1 img").css("display","none");
                    $.each(data,function (index,album) {
                        last_page = album.totalPage;
                        $('.item[data-index='+(index+1)+'] .album_item_1').html(album.singer.singerName);
                        $('.item[data-index='+(index+1)+'] .album_item_2').html(album.albumName);
                        $('.item[data-index='+(index+1)+'] img:first-child').attr('src',album.singer.poster).css("display","block").next().attr('src',album.albumPoster).css("display","block");
                        $('.showTotalPage').text("1/"+album.totalPage);
                    })
                }
            })
        }
        else{
            $.ajax({
                "url":"/AlbumPageServlet?opr=searchByArea",
                "data":"album_language="+album_language+"&page="+page+"&orderType="+orderType+"&album_area_choice="+album_area_choice,
                "type":"POST",
                "dataType":"json",
                "success":function (data) {
                    $("#clearDIV .album_item_1").empty();
                    $("#clearDIV .album_item_2").empty();
                    $("#clearDIV .img_1 img").css("display","none");
                    $(".showTotalPage").empty();
                    $.each(data,function (index,album) {
                        // alert(album.totalPage);
                        last_page = album.totalPage;
                        $('.item[data-index='+(index+1)+'] .album_item_1').html(album.singer.singerName);
                        $('.item[data-index='+(index+1)+'] .album_item_2').html(album.albumName);
                        $('.item[data-index='+(index+1)+'] img:first-child').attr('src',album.singer.poster).css("display","block").next().attr('src',album.albumPoster).css("display","block");
                        $('.showTotalPage').text("1/"+album.totalPage);
                    })
                }
            })
        }
    });
});

//选择流派
$(function () {
    $('#_show ul li a').click(function () {
        album_area_choice =$(this).attr("value");
        //alert(album_area_choice);
        if(album_area_choice == "全部"){
            album_area_choice = null;
            $.ajax({
                "url":"/AlbumPageServlet?opr=searchByArea",
                "data":"page="+page+"&orderType="+orderType+"&album_language="+album_language+"&album_area_choice="+album_area_choice,
                "type":"POST",
                "dataType":"json",
                "success":function (data) {
                    $("#clearDIV .album_item_1").empty();
                    $("#clearDIV .album_item_2").empty();
                    $("#clearDIV .img_1 img").css("display","none");
                    $(".showTotalPage").empty();
                    $.each(data,function (index,album) {
                        //alert(album.totalPage);
                        last_page = album.totalPage;
                        $('.item[data-index='+(index+1)+'] .album_item_1').html(album.singer.singerName);
                        $('.item[data-index='+(index+1)+'] .album_item_2').html(album.albumName);
                        $('.item[data-index='+(index+1)+'] img:first-child').attr('src',album.singer.poster).css("display","block").next().attr('src',album.albumPoster).css("display","block");
                        $('.showTotalPage').text("1/"+album.totalPage);
                    })
                }
            })
        }
        else{
            $.ajax({
                "url":"/AlbumPageServlet?opr=searchByArea",
                "data":"page="+page+"&orderType="+orderType+"&album_language="+album_language+"&album_area_choice="+album_area_choice,
                "type":"POST",
                "dataType":"json",
                "success":function (data) {
                    $("#clearDIV .album_item_1").empty();
                    $("#clearDIV .album_item_2").empty();
                    $("#clearDIV .img_1 img").css("display","none");
                    $(".showTotalPage").empty();
                    $.each(data,function (index,album) {
                        //alert(album.totalPage);
                        last_page = album.totalPage;
                        $('.item[data-index='+(index+1)+'] .album_item_1').html(album.singer.singerName);
                        $('.item[data-index='+(index+1)+'] .album_item_2').html(album.albumName);
                        $('.item[data-index='+(index+1)+'] img:first-child').attr('src',album.singer.poster).css("display","block").next().attr('src',album.albumPoster).css("display","block");
                        $('.showTotalPage').text("1/"+album.totalPage);
                    })
                }
            })
        }
    })
})

//选择排序
$(function () {
    $('#hot_new_show ul li a').click(function () {
        orderType =$(this).attr("value");
        //alert(orderType);
        if(album_area_choice == "全部"){
            album_area_choice = null;
            $.ajax({
                "url":"/AlbumPageServlet?opr=searchByArea",
                "data":"page="+page+"&orderType="+orderType+"&album_language="+album_language+"&album_area_choice="+album_area_choice,
                "type":"POST",
                "dataType":"json",
                "success":function (data) {
                    $("#clearDIV .album_item_1").empty();
                    $("#clearDIV .album_item_2").empty();
                    $("#clearDIV .img_1 img").css("display","none");
                    $(".showTotalPage").empty();
                    $.each(data,function (index,album) {
                        //alert(album.totalPage);
                        last_page = album.totalPage;
                        $('.item[data-index='+(index+1)+'] .album_item_1').html(album.singer.singerName);
                        $('.item[data-index='+(index+1)+'] .album_item_2').html(album.albumName);
                        $('.item[data-index='+(index+1)+'] img:first-child').attr('src',album.singer.poster).css("display","block").next().attr('src',album.albumPoster).css("display","block");
                        $('.showTotalPage').text("1/"+album.totalPage);
                    })
                }
            })
        }
        else {
            $.ajax({
                "url":"/AlbumPageServlet?opr=searchByArea",
                "data":"page="+page+"&orderType="+orderType+"&album_language="+album_language+"&album_area_choice="+album_area_choice,
                "type":"POST",
                "dataType":"json",
                "success":function (data) {
                    $("#clearDIV .album_item_1").empty();
                    $("#clearDIV .album_item_2").empty();
                    $("#clearDIV .img_1 img").css("display","none");
                    $(".showTotalPage").empty();
                    $.each(data,function (index,album) {
                        //alert(album.totalPage);
                        last_page = album.totalPage;
                        $('.item[data-index='+(index+1)+'] .album_item_1').html(album.singer.singerName);
                        $('.item[data-index='+(index+1)+'] .album_item_2').html(album.albumName);
                        $('.item[data-index='+(index+1)+'] img:first-child').attr('src',album.singer.poster).css("display","block").next().attr('src',album.albumPoster).css("display","block");
                        $('.showTotalPage').text("1/"+album.totalPage);
                    })
                }
            })
        }
    })
})
//分页
var recent_page=1;
var first_page=1;
var final_page=null;
//上一页
$(function () {
    $('.back_page').click(function () {
            if(album_language == "推荐") {
                final_page=$(".last_page").attr("value");
                if(recent_page == final_page && final_page>1){
                    recent_page = final_page-1;
                }else{
                    if(recent_page>1){
                        recent_page --;
                    }
                }
               // alert(recent_page)
                //alert(final_page)
                $.ajax({
                    "url":"/AlbumPageServlet?opr=recommand",
                    "data":"page="+recent_page,
                    "type":"POST",
                    "dataType":"json",
                    "success":function (data) {
                        // alert(eval(data.length));
                        $("#clearDIV .album_item_1").empty();
                        $("#clearDIV .album_item_2").empty();
                        $("#clearDIV .img_1 img").css("display","none");
                        $.each(data,function (index,album) {
                            last_page = album.totalPage;
                            $('.item[data-index='+(index+1)+'] .album_item_1').html(album.singer.singerName);
                            $('.item[data-index='+(index+1)+'] .album_item_2').html(album.albumName);
                            $('.item[data-index='+(index+1)+'] img:first-child').attr('src',album.singer.poster).css("display","block").next().attr('src',album.albumPoster).css("display","block");
                            $('.showTotalPage').text(recent_page+"/"+album.totalPage);
                        })
                    }
                })
            }
            else{
                if(recent_page == last_page && last_page>1){
                    recent_page = last_page-1;
                }else{
                    if(recent_page>1){
                        recent_page --;
                    }
                }
                $.ajax({
                    "url":"/AlbumPageServlet?opr=searchByArea",
                    "data":"page="+recent_page+"&orderType="+orderType+"&album_language="+album_language+"&album_area_choice="+album_area_choice,
                    "type":"POST",
                    "dataType":"json",
                    "success":function (data) {
                        $("#clearDIV .album_item_1").empty();
                        $("#clearDIV .album_item_2").empty();
                        $("#clearDIV .img_1 img").css("display","none");
                        $(".showTotalPage").empty();
                        $.each(data,function (index,album) {
                            //alert(album.totalPage);
                            last_page = album.totalPage;
                            $('.item[data-index='+(index+1)+'] .album_item_1').html(album.singer.singerName);
                            $('.item[data-index='+(index+1)+'] .album_item_2').html(album.albumName);
                            $('.item[data-index='+(index+1)+'] img:first-child').attr('src',album.singer.poster).css("display","block").next().attr('src',album.albumPoster).css("display","block");
                            $('.showTotalPage').text(recent_page+"/"+album.totalPage);
                        })
                    }
                })
            }
    })
})
//下一页
$(function () {
    $('.next_page').click(function () {
        // next_page =$(this).attr("value");
        // alert(next_page);
            if(album_language == "推荐") {
                final_page = $(".last_page").attr("value");
                if (recent_page + 1 <= final_page) {
                    recent_page++;
                    /*alert(recent_page)
                    alert(final_page)*/
                    $.ajax({
                        "url": "/AlbumPageServlet?opr=recommand",
                        "data": "page=" + recent_page,
                        "type": "POST",
                        "dataType": "json",
                        "success": function (data) {
                            // alert(eval(data.length));
                            $("#clearDIV .album_item_1").empty();
                            $("#clearDIV .album_item_2").empty();
                            $("#clearDIV .img_1 img").css("display", "none");
                            $.each(data, function (index, album) {
                                last_page = album.totalPage;
                                $('.item[data-index=' + (index + 1) + '] .album_item_1').html(album.singer.singerName);
                                $('.item[data-index=' + (index + 1) + '] .album_item_2').html(album.albumName);
                                $('.item[data-index=' + (index + 1) + '] img:first-child').attr('src', album.singer.poster).css("display", "block").next().attr('src', album.albumPoster).css("display", "block");
                                $('.showTotalPage').text(recent_page + "/" + album.totalPage);
                            })
                        }
                    })
                }
            }
            else{
                if(recent_page+1<=last_page){
                    recent_page ++;
                $.ajax({
                    "url":"/AlbumPageServlet?opr=searchByArea",
                    "data":"page="+recent_page+"&orderType="+orderType+"&album_language="+album_language+"&album_area_choice="+album_area_choice,
                    "type":"POST",
                    "dataType":"json",
                    "success":function (data) {
                        $("#clearDIV .album_item_1").empty();
                        $("#clearDIV .album_item_2").empty();
                        $("#clearDIV .img_1 img").css("display","none");
                        $(".showTotalPage").empty();
                        $.each(data,function (index,album) {
                            //alert(album.totalPage);
                            last_page = album.totalPage;
                            $('.item[data-index='+(index+1)+'] .album_item_1').html(album.singer.singerName);
                            $('.item[data-index='+(index+1)+'] .album_item_2').html(album.albumName);
                            $('.item[data-index='+(index+1)+'] img:first-child').attr('src',album.singer.poster).css("display","block").next().attr('src',album.albumPoster).css("display","block");
                            $('.showTotalPage').text(recent_page+"/"+album.totalPage);
                        })
                    }
                })
                }
        }
    })
})

//首页
$(function () {
    $('.first_page').click(function () {
        if(album_language == "推荐") {
            recent_page = first_page;
            $.ajax({
                "url":"/AlbumPageServlet?opr=recommand",
                "data":"page="+first_page,
                "type":"POST",
                "dataType":"json",
                "success":function (data) {
                    // alert(eval(data.length));
                    $("#clearDIV .album_item_1").empty();
                    $("#clearDIV .album_item_2").empty();
                    $("#clearDIV .img_1 img").css("display","none");
                    $.each(data,function (index,album) {
                        last_page = album.totalPage;
                        $('.item[data-index='+(index+1)+'] .album_item_1').html(album.singer.singerName);
                        $('.item[data-index='+(index+1)+'] .album_item_2').html(album.albumName);
                        $('.item[data-index='+(index+1)+'] img:first-child').attr('src',album.singer.poster).css("display","block").next().attr('src',album.albumPoster).css("display","block");
                        $('.showTotalPage').text("1/"+album.totalPage);
                    })
                }
            })
        }
        else{
            $.ajax({
                "url":"/AlbumPageServlet?opr=searchByArea",
                "data":"page="+first_page+"&orderType="+orderType+"&album_language="+album_language+"&album_area_choice="+album_area_choice,
                "type":"POST",
                "dataType":"json",
                "success":function (data) {
                    $("#clearDIV .album_item_1").empty();
                    $("#clearDIV .album_item_2").empty();
                    $("#clearDIV .img_1 img").css("display","none");
                    $(".showTotalPage").empty();
                    $.each(data,function (index,album) {
                        //alert(album.totalPage);
                        last_page = album.totalPage;
                        $('.item[data-index='+(index+1)+'] .album_item_1').html(album.singer.singerName);
                        $('.item[data-index='+(index+1)+'] .album_item_2').html(album.albumName);
                        $('.item[data-index='+(index+1)+'] img:first-child').attr('src',album.singer.poster).css("display","block").next().attr('src',album.albumPoster).css("display","block");
                        $('.showTotalPage').text("1/"+album.totalPage);
                    })
                }
            })
        }
    })
})

//尾页
$(function () {
    $('.last_page').click(function () {
        if(album_language == "推荐") {
            final_page = $(this).attr("value");
            recent_page = final_page;
            //alert(final_page)
            $.ajax({
                "url":"/AlbumPageServlet?opr=recommand",
                "data":"page="+final_page,
                "type":"POST",
                "dataType":"json",
                "success":function (data) {
                    // alert(eval(data.length));
                    $("#clearDIV .album_item_1").empty();
                    $("#clearDIV .album_item_2").empty();
                    $("#clearDIV .img_1 img").css("display","none");
                    $.each(data,function (index,album) {
                        last_page = album.totalPage;
                        $('.item[data-index='+(index+1)+'] .album_item_1').html(album.singer.singerName);
                        $('.item[data-index='+(index+1)+'] .album_item_2').html(album.albumName);
                        $('.item[data-index='+(index+1)+'] img:first-child').attr('src',album.singer.poster).css("display","block").next().attr('src',album.albumPoster).css("display","block");
                        $('.showTotalPage').text(album.totalPage+"/"+album.totalPage);
                    })
                }
            })
        }
        else{
            $.ajax({
                "url":"/AlbumPageServlet?opr=searchByArea",
                "data":"page="+last_page+"&orderType="+orderType+"&album_language="+album_language+"&album_area_choice="+album_area_choice,
                "type":"POST",
                "dataType":"json",
                "success":function (data) {
                    $("#clearDIV .album_item_1").empty();
                    $("#clearDIV .album_item_2").empty();
                    $("#clearDIV .img_1 img").css("display","none");
                    $(".showTotalPage").empty();
                    $.each(data,function (index,album) {
                        //alert(album.totalPage);
                        last_page = album.totalPage;
                        $('.item[data-index='+(index+1)+'] .album_item_1').html(album.singer.singerName);
                        $('.item[data-index='+(index+1)+'] .album_item_2').html(album.albumName);
                        $('.item[data-index='+(index+1)+'] img:first-child').attr('src',album.singer.poster).css("display","block").next().attr('src',album.albumPoster).css("display","block");
                        $('.showTotalPage').text(album.totalPage+"/"+album.totalPage);
                    })
                }
            })
        }
    })
})

